package org.rcy.ruicingmarket.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.rcy.framework.api.distributed.DistributedTransaction;
import org.rcy.framework.api.entity.PageResult;
import org.rcy.framework.api.entity.ResultMessage;
import org.rcy.ruicingmarket.category.CategoryClient;
import org.rcy.ruicingmarket.category.CategoryRequest;
import org.rcy.ruicingmarket.category.CategoryResponse;
import org.rcy.ruicingmarket.category.CategoryService;
import org.rcy.ruicingmarket.category.CategoryVo;
import org.rcy.ruicingmarket.exception.GoodsException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 吴康桥
 * @Description
 * @date 2022/12/20 14:44
 */

@RestController
@RequestMapping("/category")
@Tag(name = "category", description = "分类控制器")
public class CategoryController implements CategoryClient {
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private CategoryService categoryService;

	@PostMapping("/add")
	@Operation(summary = "添加分类", description = "添加分类", tags = {"category"})
	@Parameter(name = "Gateway-Token", required = true , in = ParameterIn.HEADER)
	public ResultMessage addCategory(@RequestBody CategoryVo vo){
		try {
			Long id = categoryService.addCategory(vo);
			return ResultMessage.success(id);
		} catch (GoodsException e) {
			logger.error(e.getMessage());
		}catch (Exception e){
			logger.error(e.getMessage(),e);
		}
		return ResultMessage.error();
	}

	@PostMapping("/pagequery")
	@Operation(summary = "分页查询", description = "分页查询分类", tags = {"category"})
	@Parameter(name = "Gateway-Token", required = true , in = ParameterIn.HEADER)
	public ResultMessage<PageResult<CategoryVo>> pageQuery(@RequestBody CategoryRequest request){
		try {
			PageResult<CategoryVo> pageResult = categoryService.pageQuery(request);
			return ResultMessage.success(pageResult);
		} catch (GoodsException e) {
			logger.error(e.getMessage());
		}catch (Exception e){
			logger.error(e.getMessage(),e);
		}
		return ResultMessage.error();
	}

	@Override
	@PostMapping("/test")
	public ResultMessage<CategoryResponse> testDisT(@RequestBody CategoryVo vo) {
		System.out.println("进行feign调用了");
		if(vo.getGtxId() != null) {
			DistributedTransaction transaction = new DistributedTransaction();
			transaction.setGtxId(vo.getGtxId());
			categoryService.testDx(vo,transaction);
		}
		return ResultMessage.success();
	}
}
